/*
-------------------------------------------------------------------
BIE is Copyright 2001-2003 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
*/
package com.webdeninteractive.sbie;

import java.lang.ClassLoader;
import com.webdeninteractive.sbie.util.MappedObjectFactoryBase;
import com.webdeninteractive.sbie.Service;

/**
 * Generates named Services using a given ClassLoader.
 * <p>
 * Services are mapped to names by calling <CODE>register</CODE>,
 * and are created with <CODE>makeService</CODE>.
 *
 * Example of usage:
 * <pre>
 *    ServiceFactory sf = new ServiceFactory( myClassLoader );
 *    sf.register( "translate", "com.wdi.sbie.services.TranslateService" );
 *    sf.register( "update", "com.wdi.sbie.services.UpdateService" );
 *    try {
 *      Service updateService = sf.makeService( "update" );
 *       . . .
 * </pre>
 */
public class ServiceFactory extends MappedObjectFactoryBase {

    /** Create a new ServiceFactory, which will use the given ClassLoader
     *  to create objects.
     */
    public ServiceFactory( ClassLoader loader ) {
        super(loader);
    }

    /** Create a new Service instance.  The implementation returned
     *  will be the one registered to the given name.
     */
    public Service makeService( String name )
        throws ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        Service newsvc = (Service)this.makeMappedObject(name);
        return newsvc;
    }

    public Service makeServiceByClassName( String className )
        throws ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        return (Service)loader.loadClass(className).newInstance();
    }

}
